var TimerTable;
var $Line;
function getTimerList() {
    if (TimerTable) {
        TimerTable.fnPageChange('first');
        return;
    }
    var i = 1;
    TimerTable = $("#timerList").DataTable({
        "sAjaxSource": _path + "/timer/findTimers",//服务端地址
        "sServerMethod": "POST",    //请求方式
        "sAjaxDataProp": "dataList",//服务端返回的数据名
        "fnCreatedRow": function () {
            i++;
        },
        "fnPreDrawCallback": function () {
            i = 1;
        },
        "aoColumns": [              //列（table显示的内容）
            { "sTitle": "序号", "sWidth": "2%",
                "mData": function () {
                    return i;
                }
            },
            { "sTitle": "ID", "mData": "id", "sWidth": "5%", "visible": false
//                ,"mRender": function(id) {
//                    return '<input type="text" name="id" value="' + id + '">';
//                }
            },
            { "sTitle": "任务名", "mData": "jobName", "sWidth": "15%"
//                ,"mRender": function(jobName) {
//                    return '<input type="text" name="jobName" value="' + jobName + '">';
//                }
            },
            { "sTitle": "分组", "mData": "jobGroup", "sWidth": "15%"
//                ,"mRender": function(jobGroup) {
//                    return '<input type="text" name="jobGroup" value="' + jobGroup + '">';
//                }
            },
            { "sTitle": "描述", "mData": "description", "sWidth": "10%"
//                ,"mRender": function(description) {
//                    return '<input type="text" name="description" value="' + conver(description) + '">';
//                }
            },
            { "sTitle": "Cron表达式", "mData": "cronExpression", "sWidth": "10%"
//                ,"mRender": function(cronExpression) {
//                    return '<input type="text" name="cronExpression" value="' + cronExpression + '">';
//                }
            },
            { "sTitle": "运行时入参", "mData": "strParam", "sWidth": "10%"
//                ,"mRender": function(strParam) {
//                    return '<input type="text" name="strParam" value="' + conver(strParam) + '">';
//                }
            },
            { "sTitle": "临时入参", "mData": "strParam2", "sWidth": "10%"
//                ,"mRender": function(strParam2) {
//                    return '<input type="text" name="strParam2" value="' + conver(strParam2) + '">';
//                }
            },
            { "sTitle": "状态", "mData": "status", "sWidth": "10%",
                "mRender": function (data, type, full) {
                    var status = '';
                    switch (data) {
                        case '0':
                            status = "启用";
                            break;
                        case '1':
                            status = "禁用";
                            break;
                        case '2':
                            status = "暂停";
                            break;
                        case '3':
                            status = "已删除";
                            break;
                        default:
                            break;
                    }
                    return status;
                }},
            { "sTitle": "操作", "sWidth": "15%",
                "mData": function (data) {
                    var html = new Array();
                    if("ADMIN"===data.ower) {
                        html.push('<a href="javascript:trigger(6,\'' + data.id + '\');" id="btnA" class="btn  btn-xs blue">');
                        html.push('<i class="fa fa-gift" title="执行一次"></i> </a>');
//                        html.push('<a href="javascript:oper(4,\'' + id + '\');" id="btnC" class="btn  btn-xs red">');
//                        html.push('<i class="fa fa-minus-square" title="暂停"></i> </a>');
//                        html.push('<a href="javascript:oper(5,\'' + id + '\');" id="btnE"  class="btn  btn-xs yellow">');
//                        html.push('<i class="fa fa-reply" title="恢复"></i> </a>');
                        html.push('<a href="javascript:save(\'' + data.id + '\')" class="btn default btn-xs green">');
                        html.push('<i class="fa fa-check-square-o" title="保存"></i> </a>');
                        html.push('<a href="javascript:del(\'' + data.id + '\');" id="btnDel" class="btn  btn-xs red">');
                        html.push('&nbsp;<i class="fa fa-trash-o" title="删除"></i> </a>');
                    }
                    return html.join('');
                }
            }
        ]
    });
    $('#timerList tbody').on( 'click', 'tr', function () {
        $Line = $(this);
        if ( $Line.hasClass('selected') ) {
            $Line.removeClass('selected');
        } else {
            TimerTable.$('tr.selected').removeClass('selected');
            $Line.addClass('selected');
        }
    } );
}

function save() {
    App.ajax(_path + "/timer/save", $Line.find("input").serialize()+"&"+$Line.find("select").serialize(), function(data) {
        switch(data.statusCode) {
            case 200 : App.bubble(200, "操作成功！"); window.location.reload(); break;
            case 500 : App.bubble(500, data.info);break;
            default : App.bubble(500, "操作失败！");break;
        }
    });
}
function del(id) {
    if(confirm("确认删除此定时任务？"))
        App.ajax(_path + "/timer/delete", "id="+id, function(data) {
            switch(data.statusCode) {
                case 200 : App.bubble(200, "操作成功！"); window.location.reload(); break;
                case 500 : App.bubble(500, data.info);break;
                default : App.bubble(500, "操作失败！");break;
            }
            TimerTable.row('.selected').remove().draw( false );
        });
}
function trigger() {
    App.ajax(_path + "/timer/trigger", $Line.find("input").serialize()+"&"+$Line.find("select").serialize(), function(data) {
        switch(data.statusCode) {
            case 200 : App.bubble(200, "操作成功！");break;
            case 500 : App.bubble(500, data.info);break;
            default : App.bubble(500, "操作失败！");break;
        }
    });
}
